using System;
using System.Xml;
using System.IO;
using System.Collections;

namespace GURUCORE.Framework.Core.Util.Configuration
{
	/// <summary>
	/// Util Class, Load setting from Config.config file into Configuration Class
	/// </summary>
	public class ConfigurationLoader
	{
		private Hashtable m_htSections;

		/// <summary>
		/// Init and load from XML
		/// </summary>
		/// <param name="p_sConfigFile">Config.config file name</param>
		public ConfigurationLoader(string p_sConfigFile)
		{
			m_htSections = new Hashtable();
			XmlTextReader oReader = null;
			try
			{
				oReader = new XmlTextReader(p_sConfigFile);
				XmlDocument oXmlDoc = new XmlDocument();
				oXmlDoc.Load(oReader);
				this.LoadXmlDoc(oXmlDoc);
			}
			catch (Exception ex)
			{
				GException.ProcessException(new ConfigurationException(ex));
			}
			finally
			{
				if (oReader != null)
				{
					oReader.Close();
				}
			}
		}

		
		
		public XmlDocument GetSection(string p_sName)
		{
			if (m_htSections.Contains(p_sName))
			{
				string sSectionXml = m_htSections[p_sName].ToString();
				XmlDocument oSectionXmlDoc = new XmlDocument();
				try
				{
					oSectionXmlDoc.LoadXml("<Section>" + sSectionXml + "</Section>");
				}
				catch (XmlException ex)
				{
					GException.ProcessException( new ConfigurationException(ex));
				}
				return oSectionXmlDoc;
			}
			else
			{
				return null;
			}
		}
	
		
		
		private void LoadXmlDoc(XmlDocument p_oXmlDoc)
		{
			XmlNodeList arrSectionNodes = p_oXmlDoc.GetElementsByTagName("Section");
			string sSectionName;
			foreach (XmlNode oSectionNode in arrSectionNodes)
			{
				sSectionName = oSectionNode.Attributes["name"].Value.ToString();
				m_htSections.Add(sSectionName,oSectionNode.InnerXml);
			}
		}

		
	}	// end class
}	// end namespace
